﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class DomainController : Controller
    {
        //
        // GET: /Domain/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// get domains list
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDomain()
        {
            string searchCounty = Request.Params["query"];
            DomainContainer container = DomainModels.GetDomainArray(searchCounty);
            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// add new domain
        /// </summary>
        /// <returns></returns>
        public ActionResult AddDomain()
        {
            string domain = Request.Params["data"];
			string messageBack = null;
			try
			{
				if (domain != "")
				{
					DomainModels.AddDomain(domain);
					messageBack = "Domeniul a fost adaugat cu succes!";
				}

				return Json(new { success = true, description = messageBack });
			}
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume";
				}
				return Json(new { success = true, description = messageBack });
			}
        }
    }
}
